package com.demo.sender.sms;

import com.demo.common.ResultData;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.exception.ExceptionUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import software.amazon.awssdk.auth.credentials.AwsBasicCredentials;
import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider;
import software.amazon.awssdk.awscore.exception.AwsServiceException;
import software.amazon.awssdk.core.exception.SdkClientException;
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.sns.SnsClient;
import software.amazon.awssdk.services.sns.model.*;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;

/**
 * @author wangfengchen
 * 文档地址：<a href="https://docs.aws.amazon.com/sns/latest/dg/sms_publish-to-phone.html">https://docs.aws.amazon.com/sns/latest/dg/sms_publish-to-phone.html</a>
 * <a href="https://sdk.amazonaws.com/java/api/latest/software/amazon/awssdk/services/sns/SnsClient.html#publish">https://sdk.amazonaws.com/java/api/latest/software/amazon/awssdk/services/sns/SnsClient.html#publish</a>
 * <a href="https://docs.aws.amazon.com/sns/latest/api/welcome.html">https://docs.aws.amazon.com/sns/latest/api/welcome.html</a>
 */
@Component
@Slf4j
public class AwsSmsSender implements SmsSenderService {


  @Value("${sms.aws.accessKey:not_set}")
  private String accessKey;
  @Value("${sms.aws.secretKey:not_set}")
  private String secretKey;
  @Value("${sms.aws.region:not_set}")
  private String region;

  private SnsClient snsClient;


  @PostConstruct
  public void init() {
    snsClient = SnsClient.builder()
        .region(Region.of(region))
        .credentialsProvider(StaticCredentialsProvider.create(AwsBasicCredentials.create(accessKey, secretKey)))
        .build();
    log.info("aws sns client初始化成功");
  }

  @PreDestroy
  public void destroy() {
    snsClient.close();
    log.info("aws sns client关闭成功");
  }


  @Override
  public ResultData<Object> send(String phone, String message) {
    PublishResponse response = null;
    try {
      PublishRequest request = PublishRequest.builder()
          .phoneNumber(phone)
          .message(message)
          .build();
      response = snsClient.publish(request);
    } catch (AwsServiceException | SdkClientException e) {
      log.error("发送短信异常，exception = {}", ExceptionUtils.getStackTrace(e));
      return ResultData.fail();
    }
    return ResultData.success(response.messageId());
  }

  public ResultData<Object> checkIfPhoneNumberIsOptedOut(String phone) {
    CheckIfPhoneNumberIsOptedOutRequest request = CheckIfPhoneNumberIsOptedOutRequest.builder()
        .phoneNumber(phone)
        .build();
    CheckIfPhoneNumberIsOptedOutResponse response = snsClient.checkIfPhoneNumberIsOptedOut(request);
    return ResultData.success(response);
  }

  public ResultData<Object> listPhoneNumbersOptedOut() {
    ListPhoneNumbersOptedOutResponse response = snsClient.listPhoneNumbersOptedOut();
    return ResultData.success(response.phoneNumbers());
  }

}
